package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 *治污中心
 */
@Data
public class PollutionControl implements Serializable {

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = -3857182870031545299L;
    /**
     * id
     */
    private Long id;

    /**
     * 单据编号
     */
    private String number;

    /**
     *养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 记录日期
     */
    private Date date;

    /**
     * 有无安全隐患:0:有 1:无
     */
    private Integer isPitfall;

    /**
     *安全隐患照片,如果有安全隐患，则此照片必录
     */
    @JsonIgnore
    private String pitfallPhoto;


    /**
     * 安全隐患照片, 不存数据库
     */
    @Getter
    private Map<String,Map<String,String>> pitfall;

    /**
     * 设施运行状态:
     * 0:正常;1:沼气工程设施故障;2:雨污分流设施故障;
     * 3:干湿分离设施故障;4:沼液利用设施故障
     */
    private Integer runStatus;

    /**
     * 如果运行状态不正常，需要照片(多张为宜)
     */
    @JsonIgnore
    private String abnormalPhoto;

    /**
     * 运行状态不正常，需要照片,不存数据库
     */
    @Getter
    private Map<String,Map<String,String>> abnormal;

    /**
     * 干粪利用量（千克）
     */
    private Float dryUsedQuantity;

    /**
     * 干粪利用去向
     */
    private String dryUsedTo;

    /**
     * 干粪利用人
     */
    private String dryUsedUser;

    /**
     * 沼液利用量（吨）
     */
    private Float liquidUsedQuantity;

    /**
     * 沼液利用去向
     */
    private String liquidUsedTo;

    /**
     * 沼液利用人
     */
    private String liquidUsedUser;

    /**
     * 状态,0: 未提交 1: 生效
     */
    private Integer status;
    /**
     * 1：正常，-1：删除
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


     public void setPitfallPhoto(String pitfallPhoto) throws Exception{
        this.pitfallPhoto = pitfallPhoto;
        if(Strings.isNullOrEmpty(pitfallPhoto)){
            this.pitfall=  Maps.newHashMap();
        } else{
            this.pitfall = objectMapper.readValue(pitfallPhoto,new TypeReference<Map<String,Map<String,String>>>() {});
        }
    }

    public void setPitfall(Map<String,Map<String,String>> pitfall) {
        this.pitfall = pitfall;
        if(pitfall ==null ||pitfall.isEmpty()){
            this.pitfallPhoto = null;
        }else{
            try {
                this.pitfallPhoto = objectMapper.writeValueAsString(pitfall);
            } catch (Exception e) {
            }
        }
    }

    public void setAbnormalPhoto(String abnormalPhoto) throws Exception{
        this.abnormalPhoto = abnormalPhoto;
        if(Strings.isNullOrEmpty(abnormalPhoto)){
            this.abnormal=  Maps.newHashMap();
        } else{
            this.abnormal = objectMapper.readValue(abnormalPhoto, new TypeReference<Map<String,Map<String,String>>>(){});
        }
    }
    public void setAbnormal(Map<String,Map<String,String>> abnormal) {
        this.abnormal = abnormal;
        if(abnormal ==null ||abnormal.isEmpty()){
            this.abnormalPhoto = null;
        }else{
            try {
                this.abnormalPhoto = objectMapper.writeValueAsString(abnormal);
            } catch (Exception e) {
            }
        }
    }
}
